#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   shadow.sh
# Version:    1.0
# Date:       2013/12/10
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    03系统安全功能-07身份鉴别-04验证口令信息是加密的
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/10
#             - /etc/shadow.
# ----------------------------------------------------------------------

##! @TODO: add user
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoSetup()
{
    trap DoCleanup EXIT INT 

    IsRoot
    EchoResult "root"

    CmdCheck useradd userdel

    SHADOW=/etc/shadow
    [ -e ${SHADOW} ]
    EchoResult "${SHADOW} exists"

    TEST_USER=u12345
    TEST_USER_PASSWORD=useriden789

    UserCheck "${TEST_USER}" && userdel -r "${TEST_USER}"

    AddUserPasswd "${TEST_USER}" "${TEST_USER_PASSWORD}"
    EchoResult "add user ${TEST_USER}"
}

##! @TODO: passwd
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function DoTest()
{
    PASSWORD=$(grep -w ${TEST_USER} ${SHADOW} | awk -F ":" '{print $2}')
    [ -n ${PASSWORD} ] && [ ${PASSWORD} != ${TEST_USER_PASSWORD} ]
    EchoResult "check ${SHADOW}"

}

##! @TODO: cleanup, delete user
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoCleanup()
{
    sleep 3
    UserCheck "${TEST_USER}" && userdel -r "${TEST_USER}"
}

# include lib files

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null, pls check"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/Check.sh
. ${SFROOT}/testcases/Security/lib/UserOps.sh

export LANG=C

DoSetup
DoTest
EchoResult "testcase: check /etc/shadow"
echo ""
